package com.riki.riki_push;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import androidx.annotation.NonNull;

import com.huawei.agconnect.config.AGConnectServicesConfig;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.push.HmsMessaging;
import com.riki.riki_push.util.CommonUtil;
import com.riki.riki_push.util.RomUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * RikiPushPlugin
 */
public class RikiPushPlugin implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;
    private Context context;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        context = flutterPluginBinding.getApplicationContext();
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "riki_push");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        try {
            if (call.method.equals("getPhonePlatform")) {
                getPhonePlatform(result);
            }else if (call.method.equals("getNotificationChannels")) {
                getNotificationChannels(call, result);
            }else{
                result.notImplemented();
            }
        } catch (Exception e) {
            result.notImplemented();
        }
    }


    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    // 获取厂商渠道
    private void getPhonePlatform(@NonNull Result result) {
        int phonePlatform = 0;
        if (RomUtil.isEmui()) {
            phonePlatform = 1;
        }
        if (RomUtil.isMiui()) {
            phonePlatform = 2;
        }
        if (RomUtil.isOppo()) {
            phonePlatform = 3;
        }
        if (RomUtil.isVivo()) {
            phonePlatform = 4;
        }
        if (RomUtil.isFlyme()) {
            phonePlatform = 5;
        }
        result.success(phonePlatform);
    }


    // 获取通道信息
    private void getNotificationChannels(MethodCall call, Result result) {
        List<Map<String, Object>> notificationChannelDetails = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            List<String> channelIds = call.arguments();
            if (channelIds != null && channelIds.size() > 0) {
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                for (String channelId : channelIds) {
                    NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
                    if (notificationChannel != null) {
                        Map<String, Object> channelDetails = new HashMap<>();
                        channelDetails.put("playSound", notificationChannel.getSound() != null);
                        channelDetails.put("enableLights", notificationChannel.shouldShowLights());
                        channelDetails.put("enableVibration", notificationChannel.shouldVibrate());
                        channelDetails.put("showBadge", notificationChannel.canShowBadge());
                        channelDetails.put("id", channelId);
                        notificationChannelDetails.add(channelDetails);
                    }
                }
            }
        }
        result.success(notificationChannelDetails);

    }


}
